package com.yaolong.api.modules.opms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yaolong.api.modules.opms.entity.FixResident;
import com.yaolong.api.modules.opms.dao.FixResidentDao;
import com.yaolong.api.modules.opms.entity.OpmsFixFamilyInfo;
import com.yaolong.api.modules.opms.service.FixResidentService;
import com.yaolong.api.modules.opms.service.OpmsFixFamilyInfoService;
import com.yaolong.api.modules.sys.dto.UserInfoDto;
import com.yaolong.api.modules.sys.po.RoleInfo;
import com.yaolong.api.modules.sys.service.ISysUserInfoService;
import com.yaolong.api.modules.utils.CheckRoleUtils;
import com.yaolong.commons.base2.BaseServiceImpl;
import com.yaolong.commons.exceptions.BusinessException;
import com.yaolong.commons.utils.MapperUtils;
import com.yaolong.commons.utils.ObjectTool;
import org.apache.poi.hssf.record.DVALRecord;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 居民管理 服务实现类
 * </p>
 *
 * @author yaolonga
 * @version v1.0.0
 * @since 2023-02-05
 */
@Service
public class FixResidentServiceImpl extends BaseServiceImpl<FixResidentDao, FixResident> implements FixResidentService {
    @Autowired
    private OpmsFixFamilyInfoService opmsFixFamilyInfoService;

    @Autowired
    private ISysUserInfoService iSysUserInfoService;

    @Override
    public IPage<FixResident> page(int current, int size, FixResident domain) {
        IPage<FixResident> page = new Page<>(current, size);
        //根据角色获取家庭id信息
        UserInfoDto userInfoDto = iSysUserInfoService.userInfoByContext();
        List<RoleInfo> roleInfos = MapperUtils.copyList(userInfoDto.getRoles(), RoleInfo.class);
        Long userId = userInfoDto.getUserId();
        //true:含有管理员权限 或者 审核员权限
        boolean flag = CheckRoleUtils.checkRole(roleInfos);
        if (!flag) {
            //居民角色
            List<OpmsFixFamilyInfo> list = opmsFixFamilyInfoService.list(new LambdaQueryWrapper<OpmsFixFamilyInfo>()
                    .eq(OpmsFixFamilyInfo::getUserId, userId));
            if (!list.isEmpty()) {
                domain.setFamilyId(list.get(0).getId());
            }
        }
        IPage<FixResident> page1 = lambdaQuery()
                .eq(!ObjectUtils.isEmpty(domain.getHkType()), FixResident::getHkType, domain.getHkType())
                .nested(!ObjectUtils.isEmpty(domain.getFamilyId()), i -> i.eq(FixResident::getFamilyId, domain.getFamilyId())
                        .or()
                        .eq(FixResident::getFamilyId, null)
                )
                .like(!ObjectUtils.isEmpty(domain.getName()), FixResident::getName, domain.getName())
                .like(!ObjectUtils.isEmpty(domain.getHouseholder()), FixResident::getHouseholder, domain.getHouseholder())
                .like(!ObjectUtils.isEmpty(domain.getHouseholderRelation()), FixResident::getHouseholderRelation, domain.getHouseholderRelation())
                .like(!ObjectUtils.isEmpty(domain.getIdCard()), FixResident::getIdCard, domain.getIdCard())
                .like(!ObjectUtils.isEmpty(domain.getBirthPlace()), FixResident::getBirthPlace, domain.getBirthPlace())
                .like(!ObjectUtils.isEmpty(domain.getNowAddr()), FixResident::getNowAddr, domain.getNowAddr())
                .like(!ObjectUtils.isEmpty(domain.getContactNumber()), FixResident::getContactNumber, domain.getContactNumber())
                .like(!ObjectUtils.isEmpty(domain.getCultureType()), FixResident::getCultureType, domain.getCultureType())
                .like(!ObjectUtils.isEmpty(domain.getMaritalStatus()), FixResident::getMaritalStatus, domain.getMaritalStatus())
                .like(!ObjectUtils.isEmpty(domain.getPoliticsStatus()), FixResident::getPoliticsStatus, domain.getPoliticsStatus())
                .like(!ObjectUtils.isEmpty(domain.getHjStatus()), FixResident::getHjStatus, domain.getHjStatus())
                .like(!ObjectUtils.isEmpty(domain.getAge()), FixResident::getAge, domain.getAge())
                .like(!ObjectUtils.isEmpty(domain.getEmploymentStatus()), FixResident::getEmploymentStatus, domain.getEmploymentStatus())
                .page(page);
        return page1;
    }

    @Override
    public List<FixResident> list(FixResident domain) {

        return lambdaQuery()
                .eq(!ObjectUtils.isEmpty(domain.getHkType()), FixResident::getHkType, domain.getHkType())
                .like(!ObjectUtils.isEmpty(domain.getName()), FixResident::getName, domain.getName())
                .eq(!ObjectUtils.isEmpty(domain.getHouseholder()), FixResident::getHouseholder, domain.getHouseholder())
                .like(!ObjectUtils.isEmpty(domain.getHouseholderRelation()), FixResident::getHouseholderRelation, domain.getHouseholderRelation())
                .like(!ObjectUtils.isEmpty(domain.getIdCard()), FixResident::getIdCard, domain.getIdCard())
                .like(!ObjectUtils.isEmpty(domain.getBirthPlace()), FixResident::getBirthPlace, domain.getBirthPlace())
                .like(!ObjectUtils.isEmpty(domain.getNowAddr()), FixResident::getNowAddr, domain.getNowAddr())
                .like(!ObjectUtils.isEmpty(domain.getContactNumber()), FixResident::getContactNumber, domain.getContactNumber())
                .like(!ObjectUtils.isEmpty(domain.getCultureType()), FixResident::getCultureType, domain.getCultureType())
                .like(!ObjectUtils.isEmpty(domain.getMaritalStatus()), FixResident::getMaritalStatus, domain.getMaritalStatus())
                .like(!ObjectUtils.isEmpty(domain.getPoliticsStatus()), FixResident::getPoliticsStatus, domain.getPoliticsStatus())
                .like(!ObjectUtils.isEmpty(domain.getHjStatus()), FixResident::getHjStatus, domain.getHjStatus())
                .like(!ObjectUtils.isEmpty(domain.getAge()), FixResident::getAge, domain.getAge())
                .like(!ObjectUtils.isEmpty(domain.getEmploymentStatus()), FixResident::getEmploymentStatus, domain.getEmploymentStatus())
                .list();
    }

    @Override
    public boolean create(FixResident domain) {
        try {
            if (null == domain.getFamilyId()) {
                OpmsFixFamilyInfo opmsFixFamilyInfo = new OpmsFixFamilyInfo();
                opmsFixFamilyInfo.setHkbNumber(domain.getHkbNumber());
                opmsFixFamilyInfo.setHouseholder(domain.getHouseholder());
                List<OpmsFixFamilyInfo> list = opmsFixFamilyInfoService.list(opmsFixFamilyInfo);
                if (!list.isEmpty()) {
                    domain.setFamilyId(list.get(0).getId());
                }else {
                    throw new BusinessException("输入的户主姓名有误！");
                }
            }
            return super.create(domain);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public boolean update(FixResident domain) {
        if (!ObjectTool.isEmpty(domain.getHkbNumber())) {
            OpmsFixFamilyInfo opmsFixFamilyInfo = new OpmsFixFamilyInfo();
            opmsFixFamilyInfo.setHkbNumber(domain.getHkbNumber());
            List<OpmsFixFamilyInfo> list = opmsFixFamilyInfoService.list(opmsFixFamilyInfo);
            if (!list.isEmpty()) {
                domain.setFamilyId(list.get(0).getId());
            }
        }
        return super.update(domain);
    }

}
